<template>
  <div class="main">
    <div class="avatar">
      <van-uploader
        :after-read="onUpload"
        accept="image/png,image/jpeg,image/gif,image/jpg,image/svg+xml,image/webp,image/apng,image/avif,image/bmp"
      >
        <van-image round :src="avatar" fit="cover" />
      </van-uploader>
      <div>{{ userInfo.userName }}</div>
    </div>
    <van-swipe :autoplay="3000" lazy-render>
      <van-swipe-item v-for="image in images" :key="image">
        <img :src="image" class="swipeImg" />
      </van-swipe-item>
    </van-swipe>
    <div class="tool">
      <div class="tip">我的工具</div>
      <div class="cell">
        <van-icon name="idcard" class="img" />
        <span>我的名片</span>
        <div style="flex: 1" />
        <van-icon name="arrow" />
      </div>
      <div class="cell">
        <van-icon name="friends-o" class="img" />
        <span>我的朋友圈</span>
        <div style="flex: 1" />
        <van-icon name="arrow" />
      </div>
      <div class="cell">
        <van-icon name="goods-collect-o" class="img" />
        <span>我的收藏</span>
        <div style="flex: 1" />
        <van-icon name="arrow" />
      </div>
      <div class="cell">
        <van-icon name="warn-o" class="img" />
        <span>黑名单</span>
        <div style="flex: 1" />
        <van-icon name="arrow" />
      </div>
      <div class="cell" @click="goSetting">
        <van-icon name="setting-o" class="img" />
        <span>个性化设置</span>
        <div style="flex: 1" />
        <van-icon name="arrow" />
      </div>
      <div class="cell">
        <van-icon name="phone-o" class="img" />
        <span>联系我们</span>
        <div style="flex: 1" />
        <van-icon name="arrow" />
      </div>
    </div>
    <div class="slot"></div>
  </div>
</template>

<script setup lang="ts">
import defaultAvatar from "@/assets/cat.jpeg";
import { uploadFile, uploadAvatar } from "@/api/upload";
import { ref } from "vue";
import { useRouter } from "vue-router";

const router = useRouter();
const userInfo = JSON.parse(localStorage.getItem("userInfo"));
const avatar = ref(defaultAvatar);
const images = [
  "https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg",
  "https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg",
  "https://fastly.jsdelivr.net/npm/@vant/assets/apple-3.jpeg",
  "https://fastly.jsdelivr.net/npm/@vant/assets/apple-4.jpeg",
];
if (userInfo.userAvatar) {
  avatar.value = userInfo.userAvatar;
}
const goSetting = () => {
  router.push("/setting");
};
const goMyCard = () => {
  router.push("/myIdCard");
};
const onUpload = (file) => {
  // console.log(file.file);
  const formData = new FormData();
  formData.append("file", file.file);
  uploadFile(formData).then((res) => {
    if (res.code === 200) {
      let userInfo = JSON.parse(localStorage.getItem("userInfo"));
      userInfo.userAvatar = res.path;
      localStorage.setItem("userInfo", JSON.stringify(userInfo));
      avatar.value = res.path;
      uploadAvatar({ avatarUrl: res.path, username: userInfo.userName });
    }
  });
};
</script>

<style scoped lang="scss">
.main {
  font-size: 16px;
  box-sizing: border-box;
  // background-color: rgba(189, 183, 107, 0.596);
  background: url("@/assets/me_bg.jpg") no-repeat;
  background-size: cover;
}
.cell {
  padding: 15px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #f1f1f1;
  color: #333;
  font-size: 13px;
  font-weight: bold;
  .img {
    margin-right: 5px;
    font-size: 18px;
  }
}
.tool {
  border-radius: 10px;
  margin: 20px 15px 60px;
  box-shadow: 0px 1px 10px 5px #eee;
  background-color: #fff;
  .tip {
    padding: 15px;
    font-size: 16px;
    color: #333;
    font-weight: bold;
  }
}
.slot {
  height: 40px;
  background-color: rgba(189, 183, 107, 0.596);
}
.avatar {
  padding: 20px 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin-bottom: 20px;
  & > div:nth-child(2) {
    font-size: 15px;
    background-color: darkgoldenrod;
    color: #fff;
    width: 50px;
    height: 25px;
    text-align: center;
    line-height: 25px;
  }
  .van-image {
    width: 80px;
    height: 80px;
  }
}
.swipeImg {
  width: 345px;
  height: 180px;
  object-fit: cover;
  margin: 20px 15px;
}
</style>
